var AccountHelper = {
	removeAccountRecord: function(id) {
		YAHOO.util.Connect.asyncRequest("POST", "account.php?remove", {
			success: this.handleRemoveSuccess,
			failure: this.handleRemoveFailure
		}, "id=" + id);
	},
	handleRemoveSuccess: function(req) {
		var result = YAHOO.lang.JSON.parse(req.responseText);
		
		if (result.done) {
			var rs = money.inDataTable.getRecordSet();
			
			var len = rs.getLength();
			
			for (var idx = 0; idx < len; idx++) {
				var record = rs.getRecord(idx);
				if (record.getData("id") == result.id) {
					money.inDataTable.deleteRow(idx);
					break;
				}
			}
		}
	},
	handleRemoveFailure: function(req) {
		var result = YAHOO.lang.JSON.parse(req.responseText);
		alert("Error handleRemoveFailure()");
		
	},
	showAccountRecord: function(id) {
		if (!this.accountDetailPanel) {
			YAHOO.util.Dom.get("account-detail-panel-template").style.display = "block";
			this.accountDetailPanel = new YAHOO.fx.Window("account-detail-panel-template", {
				id: "account-detail-panel",
				width: "320px",
				modal: true,
				fixedcenter: true,
				constraintoviewport: true
			});
			
			this.accountDetailPanel.render();
		}
		
		this.accountDetailPanel.show();
		new Updater(YAHOO.util.Dom.get("account-detail-panel-body"), "account.php?show&id=" + id);
	},
	initAddCategoryPanel: function(conf) {
			YAHOO.util.Dom.get(conf.templateId).style.display = "block";
			var panel = new YAHOO.widget.Panel(conf.templateId, {
				width: "320px",
				modal: true,
				fixedcenter: true,
				constraintoviewport: true
			});
			
			var select = YAHOO.util.Dom.get(conf.selectId);
			
			var fn = function(panel, select) {
				return function() {
					var val = YAHOO.util.Dom.get(conf.inputId).value;
					var opt = document.createElement("option");
					opt.innerHTML = val;
					opt.value = val;
					select.appendChild(opt);
					select.value = opt.value;
					panel.hide();
				}
			}.call(this, panel, select);
			
			var button = new YAHOO.widget.Button(conf.buttonId, {
				onclick: {
					fn: fn
				}
			});
			
			panel.render();
			
			return panel;
	},
	showAddCategoryPanel: function(type, mgr) {
		if (type == "in") {
			if (!this.inAddCategoryPanel) {
				this.inAddCategoryPanel = this.initAddCategoryPanel({
					templateId: "account-in-category-panel-template",
					selectId: "account-in-select",
					inputId: "in-add-category-input",
					buttonId: "in-add-category-ok-button"
				});
				
				//this.inAddCategoryPanel.render();
				
				if (mgr) {
					mgr.register(this.inAddCategoryPanel);
				}
			}
			this.inAddCategoryPanel.show();	
			mgr.bringToTop(this.inAddCategoryPanel);
		} else {
			if (!this.outAddCategoryPanel) {
				this.outAddCategoryPanel = this.initAddCategoryPanel({
					templateId: "account-out-category-panel-template",
					selectId: "account-out-select",
					inputId: "out-add-category-input",
					buttonId: "out-add-category-ok-button"
				});
				
				if (mgr) {
					mgr.register(this.outAddCategoryPanel);
				}
			}
			this.outAddCategoryPanel.show();
			mgr.bringToTop(this.outAddCategoryPanel);	
		}
	}
}

function RemoveAccountRecord(id) {
	AccountHelper.removeAccountRecord(id);	
}

function ShowAccountRecord(id) {
	AccountHelper.showAccountRecord(id);	
}

function ShowInAddCategoryPanel(mgr) {
	AccountHelper.showAddCategoryPanel("in", mgr);
}

function ShowOutAddCategoryPanel(mgr) {
	AccountHelper.showAddCategoryPanel("out", mgr);
}